package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.domain.SpcDataCourseParam;
import com.uwlaser.service.SpcDataCourseParamService;
import com.uwlaser.service.dto.SpcDataCourseParamDto;
import com.uwlaser.service.dto.SpcDataCourseParamQueryParam;
import com.uwlaser.service.mapper.SpcDataCourseParamMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author mx
* @date 2022-08-24
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SpcDataCourseParamService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SpcDataCourseParamServiceImpl extends CommonServiceImpl<SpcDataCourseParamMapper, SpcDataCourseParam> implements SpcDataCourseParamService {

    // private final RedisUtils redisUtils;
    private final SpcDataCourseParamMapper spcDataCourseParamMapper;

    @Override
    public PageInfo<SpcDataCourseParamDto> queryAll(SpcDataCourseParamQueryParam query, Pageable pageable) {
        IPage<SpcDataCourseParam> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SpcDataCourseParam> page = spcDataCourseParamMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, SpcDataCourseParamDto.class);
    }

    @Override
    public List<SpcDataCourseParamDto> queryAll(SpcDataCourseParamQueryParam query){
        return ConvertUtil.convertList(spcDataCourseParamMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SpcDataCourseParamDto.class);
    }

    @Override
    public SpcDataCourseParam getById(Integer id) {
        return spcDataCourseParamMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SpcDataCourseParamDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), SpcDataCourseParamDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SpcDataCourseParamDto resources) {
        SpcDataCourseParam entity = ConvertUtil.convert(resources, SpcDataCourseParam.class);
        return spcDataCourseParamMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(SpcDataCourseParamDto resources){
        SpcDataCourseParam entity = ConvertUtil.convert(resources, SpcDataCourseParam.class);
        int ret = spcDataCourseParamMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return spcDataCourseParamMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<SpcDataCourseParamDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (SpcDataCourseParamDto spcDataCourseParam : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品SN", spcDataCourseParam.getProductSn());
              map.put("产品型号", spcDataCourseParam.getProductCode());
              map.put("产品类型", spcDataCourseParam.getProductType());
              map.put("订单编号", spcDataCourseParam.getOrderCode());
              map.put("工单编号", spcDataCourseParam.getWorkOrderCode());
              map.put("产线编号", spcDataCourseParam.getLienCode());
              map.put("工段编号", spcDataCourseParam.getSectionCode());
              map.put("工位编号", spcDataCourseParam.getStationCode());
              map.put("工序编号", spcDataCourseParam.getOperationCode());
              map.put("设备编号", spcDataCourseParam.getDeviceCode());
              map.put("判断结果 0不合格 1合格", spcDataCourseParam.getParamResult());
              map.put("参数编码", spcDataCourseParam.getParamCode());
              map.put("参数名称", spcDataCourseParam.getParamName());
              map.put("参数值", spcDataCourseParam.getParamValue());
              map.put("判断范围", spcDataCourseParam.getParamRange());
              map.put("参数单位", spcDataCourseParam.getParamUnit());
              map.put("测试顺序", spcDataCourseParam.getParamNumber());
              map.put("spc主表id", spcDataCourseParam.getSpcMainId());
              map.put("过程表id", spcDataCourseParam.getCpId());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
